﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Security.Application;

namespace InhollandAirPortal {
  public partial class MasterPage : System.Web.UI.MasterPage {
    private bool loggedin = false;
    private bool CheckForDatabase = true;

    protected void Page_Load(object sender, EventArgs e) {
      if (!IsDatabaseConnectionAvailable() && CheckForDatabase) { //Als er geen database beschikbaaris en er moet gecheckt worden of de database beschikbaar is, verwijs door naar de errorpagina
        Response.Redirect(ResolveClientUrl("Error/Error.aspx"));
      } else {
        navMenu.Visible = false;
        if (this.Page.User.Identity.Name.Length > 0) { loggedin = true; }
        if (loggedin) { //Laat menu's zien bij ingelogde gebruikers
          navMenu.Visible = true;
          lblLoginInfo.Text = "Gebruikersnaam: <strong>" + Encoder.HtmlEncode(this.Page.User.Identity.Name) + "</strong>";
          lblInhollandMiles.Text = "Aantal Miles: <strong>&curren;" + new PortalUser(this.Page.User.Identity.Name).inholland_miles + "</strong>";
          lblLogout.Text = "<a href=" + ResolveUrl("~/Logout.aspx") + ">Uitloggen</a>";
        } else {
          lblLoginInfo.Text = "<div><a href=" + ResolveUrl("~/Login.aspx") + ">Inloggen</a></div><div><a href=" + ResolveUrl("~/Registreer/Registreer.aspx") + ">Registreren</a></div>";
        }
      }
    }
    private bool IsDatabaseConnectionAvailable() {
      //returns true if available
      using (LoyalityDB DB = new LoyalityDB()) {
        return DB.CheckAvailability();
      }
    }

    public void SetDBCheck(bool ACheckForDatabase){
      CheckForDatabase = ACheckForDatabase;
    }
  }
}